//
//  SPTabbarController.swift
//  Swift-demo
//
//  Created by zcp on 2021/9/27.
//

import UIKit

class SPTabbarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        tabBar.isTranslucent = false
        tabBar.backgroundImage = UIImage()
        tabBar.shadowImage = UIImage()
        initTabbars()
        selectedIndex = 0
    }
    
    func initTabbars() {
        let datas: [[String: Any]] = [
            ["title" : "首页","image" : "tab_find","selectImage" : "tab_find_selected","vc" : SPHomeViewController()],
            ["title" : "发现","image" : "tab_find","selectImage" : "tab_find_selected","vc" : SPFindViewController()],
            ["title" : "我的","image" : "tab_find","selectImage" : "tab_find_selected","vc" : SPMineViewController()],
        ]
        
        for item  in datas {
//            let dict = item as NSDictionary
            addChildVC(childVC: item["vc"] as! UIViewController, title: item["title"] as? String, image: UIImage(named: item["image"] as! String), selectImage: UIImage(named: item["selectImage"] as! String))
        }
        
    }
    func addChildVC(childVC: UIViewController,title: String?,image: UIImage?,selectImage: UIImage?) -> Void {
        childVC.title = title
        childVC.tabBarItem = UITabBarItem(title: nil, image: image?.withRenderingMode(.alwaysOriginal), selectedImage: selectImage?.withRenderingMode(.alwaysOriginal))
        
        if UIDevice.current.userInterfaceIdiom == .phone {
            childVC.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
        }

        addChild(SPNaviController(rootViewController: childVC))
    }
}

extension SPTabbarController {

    override var preferredStatusBarStyle: UIStatusBarStyle {
        guard let select = selectedViewController else { return .lightContent }
        return select.preferredStatusBarStyle
    }
}
